package IdeaGui;

import java.text.DecimalFormat;

/**
 * User: Mike
 * Date: 11/19/11
 * Time: 12:24 PM
 */
public class VaporAbv {

    private String vaporAbv;
    private String vaporAbvCorr;                                                // this is the abv but corrected to match the alcohol metere
    private double[] convTemp = {100, 98.8888888888889, 98.5, 97.3888888888889, 96.5, 95.5, 94.5, 93.5, 92.5, 92, 91, 90.5, 88, 86, 83.5, 81.2222222222222, 80.6111111111111, 80.2222222222222, 79.7777777777778, 79, 78.7777777777778, 78.3888888888889, 78.2222222222222, 78.1111111111111};
    private double[] convAbv = {0, 12, 18, 28, 36, 43, 50, 54, 59, 61, 65, 67, 75, 79, 83, 86, 87, 88, 89, 90, 91, 92, 95, 96.5};


    public VaporAbv(String columnTemp) {
        double colTempD = Double.valueOf(columnTemp);
        if (colTempD > 78.1 && colTempD <= 100)
            vaporAbv = String.valueOf(getVaporAbv(columnTemp));
        else
            vaporAbv = "0";
    }


    // pass it the temperature, then do linear interpolation between temperature points to get the vapor abv
    private double getVaporAbv(String temp) {
        Integer index = 0;
        double tempD = Double.valueOf(temp);
        while (tempD <= convTemp[index])
            index++;
        double numeratorDiff = convAbv[index - 1] - convAbv[index];        // linear interpolation
        double denomDiff = convTemp[index - 1] - convTemp[index];
        double abv = numeratorDiff / denomDiff;
        abv = (tempD - convTemp[index]) * abv + convAbv[index];
        DecimalFormat df1 = new DecimalFormat("##.#");
        abv = Double.parseDouble(df1.format(abv));
        setVaporAbvCorr(abv);
        return abv;
    }

    // this uses the equation found in batch 11 spirit run data. Check the excel doc on sheet "spirit Run Data" for the equation
    private void setVaporAbvCorr(double vabv) {
        double vAbvCorr = vabv + 0.00002 * vabv * vabv * vabv - 0.0029 * vabv * vabv + 0.0232 * vabv;
        vaporAbvCorr = String.valueOf(vAbvCorr);
    }

    public String getVaporAbv() {
        return vaporAbv;
    }

    public String getVaporAbvCorr() {
        return vaporAbvCorr;
    }

    public static void main(String[] args) {
        double factor = 10.0;
        VaporAbv v = new VaporAbv("79.2");
        for (int i = 0; i < 24 * factor; i++) {
            double t = 78.2 + i / factor;
            if (t >= 78.2 && t < 100)
                System.out.println("temp,abv ," + t + "," + v.getVaporAbv(String.valueOf(t)));
        }
    }
}
